(ns basket.core
  (:require [clojure.java.io :as io]
            [clojure.data.json :as json])
  (:import (java.io File)))

(defn ^File create-connector
  "
  创建链接字符串
  (其实就是指出保存的文件夹地址)
  "
  [^String conn]
  (let [^File file (io/file conn)]
    (when-not (.exists file)
      ;; 创建文件夹
      (.mkdirs file))
    file))

(defn drop-database
  "删除数据库"
  [^File conn]
  (when (.exists conn)
    (io/delete-file (.getAbsolutePath conn))))

(defn ^File create-table
  "
  创建一个数据表
  (其实就是创建一个文件)
  "
  [^String name
   ^File conn]
  (let [table-path (str (.getAbsolutePath conn) "/" name)
        ^File table (io/file table-path)]
    (when-not (.exists table)
      (spit table-path "" :encoding "UTF-8"))
    table))

(defn ^File drop-table
  "删除一个数据表"
  [^String name
   ^File conn]
  (let [table-path (str (.getAbsolutePath conn) "/" name)
        ^File table (io/file table-path)]
    (when (.exists table)
      (io/delete-file table-path))))

(defn insert-json
  "插入json格式的数据"
  [^File table
   ^String data]
  (spit (.getAbsolutePath table) data :encoding "UTF-8"))

(defn select-json
  "读取json格式的数据"
  [^File table]
  (slurp (.getAbsolutePath table)))